﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using System.Threading;
using DevExpress.XtraEditors.DXErrorProvider;

namespace ChinaDynastyTours.Views
{
    public partial class CashAccountModifyForm : Form
    {
        public DataTable _CashDataTable;
        private string _TourGroupID;
        private int _Position;

        public CashAccountModifyForm(DataTable signBillDataTable, int position, string tourGroupID)
        {
            InitializeComponent();

            _CashDataTable = signBillDataTable;
            _TourGroupID = tourGroupID;
            _Position = position;
        }
        
        private void CashAccountModifyForm_Load(object sender, EventArgs e)
        {
            InitGUIData();

            InitValidationRules();
        }

        private void InitGUIData()
        {
            this.BindingContext[_CashDataTable].Position = _Position;

            //与UI绑定
            tbTourgroupID.DataBindings.Add("EditValue", _CashDataTable, "TourgroupID");
            tbTypeBelong.DataBindings.Add("EditValue", _CashDataTable, "TypeBelong");
            tbBudget.DataBindings.Add("EditValue", _CashDataTable, "Budget");

            tbProviders.DataBindings.Add("Tag", _CashDataTable, "ProviderID");
            tbProviders.DataBindings.Add("EditValue", _CashDataTable, "ProviderName");

            tbApplyPayTotal.DataBindings.Add("EditValue", _CashDataTable, "ApplyPayTotal");
            tbServiceName.DataBindings.Add("EditValue", _CashDataTable, "ServiceName");
            tbPayTotal.DataBindings.Add("EditValue", _CashDataTable, "PayTotal");

        }

        private void btnProviders_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ProviderSelectedToolForm providerForm = new ProviderSelectedToolForm();
            providerForm._ProviderTypes = new ProviderTypeEnum[] { ProviderTypeEnum.宾馆, ProviderTypeEnum.餐厅, ProviderTypeEnum.飞机票, ProviderTypeEnum.火车票, ProviderTypeEnum.景点, ProviderTypeEnum.演出, ProviderTypeEnum.旅行社, ProviderTypeEnum.汽车 };

            if (providerForm.ShowDialog(this) == DialogResult.OK)
            {
                DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
                btnEdit.Tag = providerForm.ReturnProviderID;
                btnEdit.EditValue = providerForm.ReturnProviderName;
            }
        }
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider.Validate()) return;

            DataRowView drv = BindingContext[_CashDataTable].Current as DataRowView;
            //创建费用
            ExpenseEntity expense = new ExpenseEntity()
            {
                ExpenseID = int.Parse(drv["ExpenseID"].ToString()), /*重点字段*/
                ServiceName = tbServiceName.Text,
                Budget = tbBudget.EditValue == null ? 0 : decimal.Parse(tbBudget.EditValue.ToString()),
                PayTotal = tbPayTotal.EditValue == null ? 0 : decimal.Parse(tbPayTotal.EditValue.ToString()),
                ApplyPayTotal = tbApplyPayTotal.EditValue == null ? 0 : decimal.Parse(tbApplyPayTotal.EditValue.ToString()),
            };
            ExpensesCtrl.UpdateExpenseDataForGuideAccount(expense);

            this.Close();

            MessageBox.Show("保存成功！", "保存数据", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 控件数据校验
        /// </summary>
        private void InitValidationRules()
        {
            // <notEmptyTextEdit> 
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "不能为空";
            // </notEmptyTextEdit>

            //不能为空
            dxValidationProvider.SetValidationRule(tbProviders, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbTypeBelong, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbServiceName, notEmptyValidationRule);

        }


    }
}
